use std::collections::HashMap;

struct Solution {
}
impl Solution {
    //使用hashMap更快
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
        //存放结果
        let mut result: Vec<i32> = Vec::new();
        //定义空HashMap存放中间值
        let mut cache_map = HashMap::new();
        for (index, item) in nums.iter().enumerate() {
            let sub = target - *item;
            match cache_map.get(item) {
                Some(index_sub) => {
                    result.push(*index_sub);
                    result.push(index as i32);
                    return result;
                }
                None => {
                    cache_map.insert(sub, index as i32);
                }
            }
        }
        result
    }
}

fn main() {
    let nums: Vec<i32> = vec![2,7,11,15];
    let target = 9;
    let solution = Solution::two_sum(nums, target);
    println!("{:?}", solution);
}
